package cn.nuist.service.impl;

import cn.nuist.dao.BillDao;
import cn.nuist.dao.CustomerDao;
import cn.nuist.dao.ProductDao;
import cn.nuist.dao.SupplierDao;
import cn.nuist.entity.OrderItems;
import cn.nuist.entity.OrderSummary;
import cn.nuist.entity.Products;
import cn.nuist.service.BillService;
import cn.nuist.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class BillServiceImpl implements BillService {
    private BillDao billDao;
    private CustomerDao customerDao;

    private ProductDao productDao;
    private SupplierDao supplierDao;


    @Autowired
    public BillServiceImpl(BillDao billDao, CustomerDao customerDao, ProductDao productDao, SupplierDao supplierDao) {
        this.billDao = billDao;
        this.customerDao = customerDao;
        this.productDao = productDao;
        this.supplierDao = supplierDao;
    }

    @Override
    public Page<OrderSummary> getViewPage(Integer pageNum) {
        Integer count = billDao.getCount();
        Page<OrderSummary> page = new Page<>();
        List<OrderSummary> list = billDao.getByPage((pageNum - 1) * page.getPageSize(),page.getPageSize());
        page.setList(list);
        page.setTotalCount(count);
        page.setCurrentPage(pageNum);
        return page;
    }

    @Override
    public List<OrderItems> getdetail(Integer orderID) {
        return billDao.getdetail(orderID);
    }

    @Override
    public Page<OrderSummary> getBillByName(Integer pageNum,String cutomerName) {
        Integer count = billDao.getCountByName(cutomerName);
        Page<OrderSummary> page = new Page<>();
        List<OrderSummary> list = billDao.getBillByName((pageNum - 1) * page.getPageSize(),page.getPageSize(),cutomerName);
        page.setList(list);
        page.setTotalCount(count);
        page.setCurrentPage(pageNum);
        return page;
    }

    @Override
    public void add(Model model) {
        model.addAttribute("customers",customerDao.getAllCustomer());
        model.addAttribute("products",productDao.getAll());
        model.addAttribute("suppliers",supplierDao.getAllSupplier());
    }
}
